package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.HomeFloorGoodsListVO;
import com.cskaoyan.bean.vo.HomeVO;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.HomeService;
import com.cskaoyan.util.MySystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: Fengyi
 * @description:
 * @time: 2021/7/11 21:36
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    MallAdMapper mallAdMapper;
    @Autowired
    MallBrandMapper mallBrandMapper;
    @Autowired
    MallGoodsMapper mallGoodsMapper;
    @Autowired
    MallTopicMapper mallTopicMapper;
    @Autowired
    MallCouponMapper mallCouponMapper;
    @Autowired
    MallCategoryMapper mallCategoryMapper;
    @Autowired
    MySystemUtil mySystemUtil;

    @Override
    public HomeVO queryAll() {
        Map<String, String> systemMap = mySystemUtil.systemMap();

        HomeVO homeVO = new HomeVO();
        MallAdExample mallAdExample = new MallAdExample();




        //MallCouponExample
        MallCouponExample mallCouponExample = new MallCouponExample();
        MallCouponExample.Criteria couponExampleCriteria = mallCouponExample.createCriteria();
        mallCouponExample.setOrderByClause("id desc limit 4" );
        couponExampleCriteria.andDeletedEqualTo(false);

        couponExampleCriteria.andStatusEqualTo( 0);

        couponExampleCriteria.andTotalGreaterThanOrEqualTo( 0);

        List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);
        homeVO.setCouponList(mallCoupons);



        //mallCategoryExample
        MallCategoryExample mallCategoryExample = new MallCategoryExample();
        MallCategoryExample.Criteria categoryExampleCriteria = mallCategoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(0);
//        List<Integer> pids = mallCategoryMapper.selectPids();
        categoryExampleCriteria.andPidEqualTo(0);
        Integer catlogList = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_catlog_list"));
        mallCategoryExample.setOrderByClause("id desc limit " + catlogList.toString());


        //homeVO赋值
        homeVO.setBanner(mallAdMapper.selectByExample(mallAdExample));
        Integer indexBrand = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_index_brand"));
        homeVO.setBrandList(mallBrandMapper.selectBrandByLimit4(indexBrand));

        Integer indexNew = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_index_new"));
        homeVO.setNewGoodsList(mallGoodsMapper.selectNewGoodsListVOByLimit6(indexNew));
        Integer indexHot = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_index_hot"));
        homeVO.setHotGoodsList(mallGoodsMapper.selectHotGoodsListVOByLimit6(indexHot));
        Integer indexTopic = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_index_topic"));
        homeVO.setTopicList(mallTopicMapper.selectTopicListByLimit4(indexTopic));




        List<MallCategory> mallCategories = mallCategoryMapper.selectByExample(mallCategoryExample);
        homeVO.setChannel(mallCategories);


        ArrayList<HomeFloorGoodsListVO> homeFloorGoodsListVOS = new ArrayList<>();
        for (MallCategory mallCategory : mallCategories) {

            //
            List<Integer> childId = mallCategoryMapper.selectChildBypid(mallCategory.getId());
            childId.add(mallCategory.getId());
            //mallGoodsExample
            MallGoodsExample mallGoodsExample = new MallGoodsExample();
            MallGoodsExample.Criteria mallGoodsExampleCriteria = mallGoodsExample.createCriteria();
            mallGoodsExampleCriteria.andDeletedEqualTo(false);
            Integer wxCatlogGoods = Integer.parseInt(systemMap.get("cskaoyan_mall_wx_catlog_goods")) ;
            mallGoodsExample.setOrderByClause("id desc limit " + wxCatlogGoods.toString());
            mallGoodsExampleCriteria.andCategoryIdIn(childId);

            HomeFloorGoodsListVO homeFloorGoodsListVO = new HomeFloorGoodsListVO();
            homeFloorGoodsListVO.setId(mallCategory.getId());
            homeFloorGoodsListVO.setName(mallCategory.getName());
            homeFloorGoodsListVO.setGoodsList(mallGoodsMapper.selectByExample(mallGoodsExample));
            homeFloorGoodsListVOS.add(homeFloorGoodsListVO);


        }
        homeVO.setFloorGoodsList(homeFloorGoodsListVOS);

        return homeVO;
    }
}
